package com.woniuxy.carlife.carlifeuserweb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.carlife.carlifecommonsweb.exception.CarlifeGlobalException;
import com.woniuxy.carlife.carlifeuserweb.entity.Shoppingcart;
import com.woniuxy.carlife.carlifeuserweb.entity.Storefavorites;
import com.woniuxy.carlife.carlifeuserweb.mapper.StorefavoritesMapper;
import com.woniuxy.carlife.carlifeuserweb.service.StatisticsService;
import com.woniuxy.carlife.carlifeuserweb.service.StorefavoritesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zjq
 * @ClassName:StorefavoritesServiceImpl
 * @Description:门店收藏
 * @date 2021/4/6 20:02
 **/

@Service
public class StorefavoritesServiceImpl extends ServiceImpl<StorefavoritesMapper, Storefavorites> implements StorefavoritesService {

    @Resource
    StorefavoritesMapper storefavoritesMapper;

    @Resource
    StatisticsService statisticsService;

    /*
     * 功能描述〈查询门店收藏，根据用户id〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/12 14:14
     * @throws
     */

    @Override
    public List<Storefavorites> findStorefavorites(Integer userId) {

        QueryWrapper<Storefavorites> storefavoritesQueryWrapper = new QueryWrapper<>();
        storefavoritesQueryWrapper.eq("user_id", userId);
        List<Storefavorites> storefavorites = storefavoritesMapper.selectList(storefavoritesQueryWrapper);
        return storefavorites == null ? null : storefavorites;
    }

    /*
     * 功能描述〈删除门店收藏〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/14 14:45
     * @throws
     */

    @Override
    public Boolean deleteStorefavoritesByStorefavoritesId(Integer storefavoritesId) {
        int i = storefavoritesMapper.deleteById(storefavoritesId);
        return i > 0 ? true : false;
    }

    /*
     * 功能描述〈门店的新增〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/14 19:48
     * @throws
     */

    @Override
    public Boolean addStorefavoritesByUserId(Storefavorites storefavorites) {
        int insert = storefavoritesMapper.insert(storefavorites);
        if (insert > 0) {
            boolean b = statisticsService.modifyFavorites(storefavorites.getUserId());
            if (b) {
                return true;
            }
        }
        return false;
    }

    /*
     * 功能描述〈门店总数统计〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/15 16:02
     * @throws
     */
    @Override
    public Integer storefavoritesTotalByUserId(Integer userId) {
        QueryWrapper<Storefavorites> shoppingcartQueryWrapper = new QueryWrapper<>();
        // QueryWrapper<Employee> queryWrapper2=Wrappers.<Employee>query();
        shoppingcartQueryWrapper.eq("user_id", userId);
        Integer count = storefavoritesMapper.selectCount(shoppingcartQueryWrapper);
        return count;
    }
}
